package com.yitiao.data_algorithms.leetcode.editor.cn;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class FindFirstAndLastPositionOfElementInSortedArray{
    // 2022-08-22 18:04:27
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] searchRange(int[] nums, int target) {
        // 先找到第一个等于8的位置，再找到第一个大于8的位置，再相减
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] >= target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        int l = right;
        left = 0;
        right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        if ((left-l-1)==0){
            return new int[]{-1,-1};
        }else {
            return new int[]{l+1,left-1};
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}